package cn.edu.jxau.test;

import java.io.IOException;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) throws IOException {

		System.out.println(uglyNumber(1500));
	}

	public static int uglyNumber(int n) {

		int[] uglyArr = new int[n]; // 数组中最后一个数即第n个丑数
		uglyArr[0] = 1;
		int curMax = uglyArr[0];
		int next = 1;
		int p2 = 0;
		int p3 = 0;
		int p5 = 0;
		while (next < n) {

			uglyArr[next] = min(uglyArr[p2] * 2, uglyArr[p3] * 3, uglyArr[p5] * 5);
			if (uglyArr[next] == uglyArr[p2] * 2) {
				p2++;
			}
			if (uglyArr[next] == uglyArr[p3] * 3) {
				p3++;
			}
			if (uglyArr[next] == uglyArr[p5] * 5) {
				p5++;
			}
			next++;
		}
		System.out.println(Arrays.toString(uglyArr));
		return uglyArr[n - 1];
	}

	private static int min(int a, int b, int c) {

		int min = a;
		if (min > b) {
			min = b;
		}
		if (min > c) {
			min = c;
		}
		return min;
	}
}